package com.moli.iov.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.sys.mapper.RolePermissionMapper;
import com.moli.iov.sys.model.dto.req.RoleDto;
import com.moli.iov.sys.model.po.RolePermission;
import com.moli.iov.sys.service.IRolePermissionService;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 角色权限业务
 * </p>
 *
 * @author zql
 * @since 2019/9/6 14:23
 */
@Slf4j
@Component
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService{

  @Autowired
  private RolePermissionMapper rolePermissionMapper;


  /**
   * 查询角色权限列表
   * @param roleDto
   * @return
   */
  @Override
  public List<RolePermission> listRolePermission(RoleDto roleDto) {
    QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("role_id", roleDto.getRoleId());
    return super.list(queryWrapper);
  }

  /**
   * 批量添加角色权限
   * @param rolePermissions
   * @return
   */
  @Override
  public boolean saveBatchRolePermission(List<RolePermission> rolePermissions) {
    return super.saveBatch(rolePermissions);
  }

  /**
   * 删除角色权限
   * @param roleId
   * @return
   */
  @Override
  public boolean deleteRolePermission(Long roleId) {
    QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("role_id", roleId);
    return rolePermissionMapper.delete(queryWrapper) > 0;
  }

  /**
   * 获取权限ids
   * @param roleIds
   * @return
   */
  @Override
  public List<Long> listPermissionIds(List<Long> roleIds) {
    QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
    queryWrapper.in("role_id", roleIds);
    List<RolePermission> rolePermissions = super.list(queryWrapper);
    List<Long> permissionIds = new ArrayList<>();
    rolePermissions.forEach(rp -> permissionIds.add(rp.getPermissionId()));
    return permissionIds;
  }
}
